package com.tjsiqi.controller;

import com.tjsiqi.beans.PageQuery;
import com.tjsiqi.common.JsonData;
import com.tjsiqi.model.StaffCheckInEXP;
import com.tjsiqi.param.StaffCheckInParam;
import com.tjsiqi.service.SysStaffCheckInService;
import com.tjsiqi.service.SysStaffCheckInService;
import com.tjsiqi.util.DateTimeUtil;
import com.tjsiqi.util.ExportExcelUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

/**
 * Created by lxc
 */

@Controller
@RequestMapping("/sys/StaffCheckIn")
public class SysStaffCheckInController {

    @Resource
    private SysStaffCheckInService sysStaffCheckInService;
// 跳转到考勤界面
    @RequestMapping("/StaffCheckIn.page")
    public ModelAndView StaffCheckInPage() {
        return new ModelAndView("StaffCheckIn");
    }

//  查询考勤记录
    @RequestMapping("/page.json")
    @ResponseBody
    public JsonData searchPage(StaffCheckInParam staffCheckInParam, PageQuery page) {
        System.out.println(staffCheckInParam);
        return JsonData.success(sysStaffCheckInService.searchPageList(staffCheckInParam, page));
    }
//  导出考勤表
    @RequestMapping("/export")
    @ResponseBody
    public JsonData exportByParam(HttpServletRequest request, HttpServletResponse response, StaffCheckInParam staffCheckInParam) throws IOException {
        String filename = "考勤表" + DateTimeUtil.dateToStr(new Date(), "yyyy-MM-dd");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8") + ".xls");
        response.setContentType("application/x-download");
        ExportExcelUtil<StaffCheckInEXP> util = new ExportExcelUtil<StaffCheckInEXP>();
        List<StaffCheckInEXP> StaffCheckInList = sysStaffCheckInService.exportByParam(staffCheckInParam);
        OutputStream ouputStream = response.getOutputStream();
        String[] columnNames = {"打卡人", "打卡时间", "备注", "IP", "单位名称"};
        util.exportExcel("考勤表", columnNames, StaffCheckInList, ouputStream, ExportExcelUtil.EXCEL_FILE_2003);
        return JsonData.success();
    }


}
